import { _decorator, Component, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { DevelopState, Soldier } from '../entity/Soldier';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { DevelopBlock } from './DevelopBlock';
import { EventManager } from '../event/EventManager';
import { DevelopUpGradeBlock } from './DevelopUpGradeBlock';
const { ccclass, property } = _decorator;

@ccclass('DevelopDetail')
export class DevelopDetail extends Component {
    @property(Sprite)
    border:Sprite
    @property(Sprite)
    cover:Sprite
    @property(Sprite)
    lock:Sprite
    @property(Node)
    close:Node
    @property(Node)
    mask:Node
    @property(Label)
    soldierName:Label
    @property(Label)
    developStr:Label
    @property(Node)
    content:Node;
    @property(Prefab)
    developBlock:Prefab
    @property(Prefab)
    developUpGradeBlock:Prefab
    @property(SpriteFrame)
    border1:SpriteFrame
    @property(SpriteFrame)
    border2:SpriteFrame
 

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)

        EventManager.instance.on(EventManager.EventType.DevelopComplete, this.setData, this)
        EventManager.instance.on(EventManager.EventType.AdvancedComplete, this.setData, this)
    }
    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.DevelopComplete, this.setData, this)
        EventManager.instance.off(EventManager.EventType.AdvancedComplete, this.setData, this)
    }

    async setData(soldier:Soldier) {
        this.content.destroyAllChildren()

        this.cover.spriteFrame = await soldier.getHeadImg()
        this.soldierName.string = soldier.name
        if(soldier.developed == DevelopState.Developed){
            this.lock.node.active = false
            this.developStr.string = '兵种已研发'

            // 添加攻击的块
            let up = instantiate(this.developUpGradeBlock)
            this.content.addChild(up)
            up.getComponent(DevelopUpGradeBlock).setDataAttack(soldier)

            // 添加防御的块
            up = instantiate(this.developUpGradeBlock)
            this.content.addChild(up)
            up.getComponent(DevelopUpGradeBlock).setDataDefense(soldier)
        } else {
            this.border.spriteFrame = this.border2

            if(soldier.getDevelopingEvent()){
                this.developStr.string = '兵种研发中'
            } else {
                this.developStr.string = '兵种未研发'
            }

           
            let db = instantiate(this.developBlock)
            this.content.addChild(db)
            db.getComponent(DevelopBlock).setData(soldier)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.DevelopDetail)
    }
}


